#include "uart.h"
#include "xn297l.h"

u8 uartBuf[BUF_LENGTH] = {0};
u8 *pWrite = uartBuf;
u8 *pRead = uartBuf;

void myUartInit()
{
    UART1_DeInit(); //清除之前的串口配置
    //不知道为什么调用自带的函数不行，只能把内容复制出来用
    uint32_t BaudRate_Mantissa = 0, BaudRate_Mantissa100 = 0;
    u32 BaudRate = 921600;//115200;
    u8 WordLength = UART1_WORDLENGTH_8D;
    u8 StopBits = UART1_STOPBITS_1;
    u8 Parity = UART1_PARITY_NO;
    u8 Mode = UART1_MODE_TXRX_ENABLE;
    u8 SyncMode = UART1_SYNCMODE_CLOCK_DISABLE;
    /* Check the parameters */
    assert_param(IS_UART1_BAUDRATE_OK(BaudRate));
    assert_param(IS_UART1_WORDLENGTH_OK(WordLength));
    assert_param(IS_UART1_STOPBITS_OK(StopBits));
    assert_param(IS_UART1_PARITY_OK(Parity));
    assert_param(IS_UART1_MODE_OK((uint8_t)Mode));
    assert_param(IS_UART1_SYNCMODE_OK((uint8_t)SyncMode));

    /* Clear the word length bit */
    UART1->CR1 &= (uint8_t)(~UART1_CR1_M);  
    
     /* Set the word length bit according to UART1_WordLength value */
    UART1->CR1 |= (uint8_t)WordLength;

    /* Clear the STOP bits */
    UART1->CR3 &= (uint8_t)(~UART1_CR3_STOP);  
    /* Set the STOP bits number according to UART1_StopBits value  */
    UART1->CR3 |= (uint8_t)StopBits;  

    /* Clear the Parity Control bit */
    UART1->CR1 &= (uint8_t)(~(UART1_CR1_PCEN | UART1_CR1_PS  ));  
    /* Set the Parity Control bit to UART1_Parity value */
    UART1->CR1 |= (uint8_t)Parity;  

    /* Clear the LSB mantissa of UART1DIV  */
    UART1->BRR1 &= (uint8_t)(~UART1_BRR1_DIVM);  
    /* Clear the MSB mantissa of UART1DIV  */
    UART1->BRR2 &= (uint8_t)(~UART1_BRR2_DIVM);  
    /* Clear the Fraction bits of UART1DIV */
    UART1->BRR2 &= (uint8_t)(~UART1_BRR2_DIVF);  

    /* Set the UART1 BaudRates in BRR1 and BRR2 registers according to UART1_BaudRate value */
    BaudRate_Mantissa    = ((uint32_t)CLK_GetClockFreq() / (BaudRate << 4));
    BaudRate_Mantissa100 = (((uint32_t)CLK_GetClockFreq() * 100) / (BaudRate << 4));
    /* Set the fraction of UART1DIV  */
    UART1->BRR2 |= (uint8_t)((uint8_t)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100)) << 4) / 100) & (uint8_t)0x0F); 
    /* Set the MSB mantissa of UART1DIV  */
    UART1->BRR2 |= (uint8_t)((BaudRate_Mantissa >> 4) & (uint8_t)0xF0); 
    /* Set the LSB mantissa of UART1DIV  */
    UART1->BRR1 |= (uint8_t)BaudRate_Mantissa;           

    /* Disable the Transmitter and Receiver before seting the LBCL, CPOL and CPHA bits */
    UART1->CR2 &= (uint8_t)~(UART1_CR2_TEN | UART1_CR2_REN); 
    /* Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */
    UART1->CR3 &= (uint8_t)~(UART1_CR3_CPOL | UART1_CR3_CPHA | UART1_CR3_LBCL); 
    /* Set the Clock Polarity, lock Phase, Last Bit Clock pulse */
    UART1->CR3 |= (uint8_t)((uint8_t)SyncMode & (uint8_t)(UART1_CR3_CPOL | 
                                              UART1_CR3_CPHA | UART1_CR3_LBCL));  

    if ((uint8_t)(Mode & UART1_MODE_TX_ENABLE))
    {
        /* Set the Transmitter Enable bit */
        UART1->CR2 |= (uint8_t)UART1_CR2_TEN;  
    }
    else
    {
        /* Clear the Transmitter Disable bit */
        UART1->CR2 &= (uint8_t)(~UART1_CR2_TEN);  
    }
    if ((uint8_t)(Mode & UART1_MODE_RX_ENABLE))
    {
        /* Set the Receiver Enable bit */
        UART1->CR2 |= (uint8_t)UART1_CR2_REN;  
    }
    else
    {
        /* Clear the Receiver Disable bit */
        UART1->CR2 &= (uint8_t)(~UART1_CR2_REN);  
    }
    /* Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit Clock 
           pulse bits according to UART1_Mode value */
    if ((uint8_t)(SyncMode & UART1_SYNCMODE_CLOCK_DISABLE))
    {
        /* Clear the Clock Enable bit */
        UART1->CR3 &= (uint8_t)(~UART1_CR3_CKEN); 
    }
    else
    {
        UART1->CR3 |= (uint8_t)((uint8_t)SyncMode & UART1_CR3_CKEN);
    }
    
    UART1_Cmd(ENABLE);  //启用串口
    UART1_ITConfig(UART1_IT_RXNE, ENABLE);//启用中断
    enableInterrupts();//开启总中断
}

const uint8_t powerTest[12] = {RF_30dBm, RF_18dBm, RF_10dBm, RF_6dBm, RF_3dBm, RF0dBm, RF2dBm, RF4dBm, RF5dBm, RF7dBm, RF8dBm, RF10dBm};
//UART串口有接收数据中断，会置位接收标志，平常我们响应中断，一般都会清除中断标志，
//但串口中断接收数据，我们都会去读取串口接收数据，读取数据会自动清除标志位
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
#if !(TEST_CH)
    *pWrite++ = UART1->DR;//读取数据会自动清除标志位
    if(pWrite - uartBuf == BUF_LENGTH)
    {
        pWrite = uartBuf;
    }
#else
    uint8_t t_data = UART1->DR;
    static uint8_t status = 0;
    static uint8_t ch = 0;
    static uint8_t powerIndex = 5;
    
    switch(status)
    {
        case 0:
            if (t_data == 't')
            {
                status++;
            }
        break;
        case 1:
            if (t_data == 'e')
            {
                status++;
            }
            else
            {
                status = 0;
            }
        break;
        case 2:
            if (t_data == 's')
            {
                status++;
            }
            else
            {
                status = 0;
            }
        break;
        case 3:
            if (t_data == 't')
            {
                status++;
            }
            else
            {
                status = 0;
            }
        break;
        case 4:
            UART1->DR = t_data;
            if (t_data < 100)
            {
                if (t_data < 81)
                {
                    uint8_t power = powerTest[powerIndex] | DATA_RATE;
                    ch = t_data;
                    xn297lCarrier(ch, power);
                }
                
            }
            else
            {
                t_data -= 100;
                if (t_data < 12)
                {
                    powerIndex = t_data;
                    uint8_t power = powerTest[powerIndex] | DATA_RATE;
                    xn297lCarrier(ch, power);
                }
            }
            status = 0;
        break;
    }
#endif
}

void uartRead(u8 *data, u8 length)
{
    static u16 count = 0;

    u8 t_length = pWrite - pRead;
    if(t_length)
    {
        while(pWrite != pRead)
        {
            pRead++;
            if(pRead - uartBuf == BUF_LENGTH)
            {
                pRead = uartBuf;
            }
        }
        count = 1;
    }
    //给一定的延时来判断输入的长度，不能太短
    //太短可能得到的值小了
    //太长得到的值又大了
    //目前测试100HZ下正常
    if(count)
    {
        count++;
        if(count > 5)
        {
            count = 0;

            if(pWrite - uartBuf < length)
            {
                length = pWrite - uartBuf;
            }
            for(u8 i = 0; i < length; i++)
            {
                *(data + i) = *(uartBuf + i);
            }
            pRead = pWrite = uartBuf;
        }
                
    }
    
    
}

void sendData(u8 *data, u8 length)
{
    for(u8 i = 0; i < length; i++)
    {
        while((UART1->SR & 0x80) == 0);//等待发送为空
        UART1->DR = *data++;
    }
}

void testRecvToSend()
{
    while(pWrite != pRead)
    {
        while((UART1->SR & 0x80) == 0);//等待发送为空
        UART1->DR = *pRead++;
        if(pRead - uartBuf == BUF_LENGTH)
        {
            pRead = uartBuf;
        }
    }
}

